/*
 * Copyright:  2018 smarabbit studio.
 *
 *  Licensed under the Confluent Community License; you may not use this file
 *  except in compliance with the License.  You may obtain a copy of the License at
 *
 *  http://www.confluent.io/confluent-community-license
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 *  WARRANTIES OF ANY KIND, either express or implied.  See the License for the
 *  specific language governing permissions and limitations under the License.
 *
 *  @作   者： 黄开晖 (117227773@qq.com)
 *  @日   期:  2020年9月2日
 */
package com.massyframework.beanskin.launcher.deploying;

import java.io.File;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.massyframework.beanskin.ModuleIdentifier;
import com.massyframework.beanskin.launcher.scanning.ModuleResources;
import com.massyframework.beanskin.launcher.scanning.maven.DefaultMavenProjectRepository;
import com.massyframework.beanskin.launcher.scanning.maven.MavenProject;
import com.massyframework.beanskin.launcher.scanning.maven.MavenProjectRepository;
import com.massyframework.beanskin.util.localstore.LocalStore;


/**
 * 调试模式下模块URL定位器
 * @author huangkh
 *
 */
class DebugModelModuleURLLocator extends DefaultModuleURLLocator {
	
	private DefaultMavenProjectRepository projectRepository;
	private Map<String, List<ModuleResources>> pomMap;

	/**
	 * 构造方法
	 */
	public DebugModelModuleURLLocator(LocalStore localStore) {
		super(localStore);
		this.pomMap = new HashMap<>();
		
		this.projectRepository = new DefaultMavenProjectRepository();
		this.projectRepository.init();
	}
	
	
	@Override
	public List<URL> locate(ModuleIdentifier module) {
		List<ModuleResources> mrs = this.pomMap.get(module.toIdentifier());
		if (mrs != null) {
			if (mrs.size() == 1) {
				MavenProject project = this.projectRepository.findMavenProject(mrs.get(0));
				if (project != null) {
					List<URL> result = project.getOutputFiles(module);
					if (!result.isEmpty()) {
						return result;
					}
				}
			}else {
				List<URL> result = null;
				for (ModuleResources resources: mrs) {
					MavenProject project = this.projectRepository.findMavenProject(resources);
					if (project != null) {
						List<URL> urls = project.getOutputFiles(module);
						if (!urls.isEmpty()) {
							if (result == null) {
								result = new ArrayList<>();
							}
							result.addAll(urls);
						}
					}
				}
				
				return result;
			}
			
		}
		
		return super.locate(module);
	}



	/**
	 * 添加pomFile
	 * @param namedVerseion {@link NamedVersion} 
	 * @param resources {@link MOduleResources}
	 * @param pomFile {@link File}
	 */
	public void addModuleResources(ModuleIdentifier module, ModuleResources resources) {
		List<ModuleResources> files = this.pomMap.computeIfAbsent(
				module.toIdentifier(), key -> new ArrayList<ModuleResources>());
		files.add(resources);
	}

	/**
	 * Maven项目仓储
	 * @return {@link MavenProjectRepository}
	 */
	public MavenProjectRepository getMavenProjectRepository() {
		return this.projectRepository;
	}
}
